package com.wn.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.RoleMapper;
import com.wn.mapper.SchedulingMapper;
import com.wn.mapper.UserMapper;
import com.wn.pojo.Drug;
import com.wn.pojo.Role;
import com.wn.pojo.Scheduling;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * 人事业务类
 */
@Service
public class HumenService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private SchedulingMapper schedulingMapper;

    /**
     * 人员信息录入
     */
    @PostMapping("add-person")
    public ServerResponse addHumen(User user) {
        //添加之前先查，如果有进不插入，没有再插入
        User someByPage = userMapper.searchSomeByPage(user);
        if (StringUtils.isEmpty(someByPage)) {
            //查询是空的，再插入
            int i = userMapper.insertSelective(user);
            return new ServerResponse(ResponseCode.SUCCESS, "添加成功");

        } else {
            return new ServerResponse(ResponseCode.ERROR, "该条信息已存在，请勿重复添加");
        }

    }

    /**
     * 修改人员信息
     *
     * @param user
     */
    public ServerResponse updateHumen(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");

        } else {
            return new ServerResponse(ResponseCode.ERROR, "修改失败");
        }

    }

    /**
     * 物理删除
     *
     * @param id
     */
    public ServerResponse deleteOneTrue(Integer id) {
        return userMapper.deleteByPrimaryKey(id) == 1 ? new ServerResponse(ResponseCode.SUCCESS, "物理删除成功") : new ServerResponse(ResponseCode.ERROR, "物理删除失败");

    }

    /**
     * 逻辑删除
     *
     * @param user
     */
    public ServerResponse deleteLogical(User user) {
        user.setIsDeleted(1);
        return userMapper.updateByPrimaryKeySelective(user) == 1 ? new ServerResponse(ResponseCode.SUCCESS, "逻辑删除成功") : new ServerResponse(ResponseCode.ERROR, "逻辑删除失败");

    }

    /**
     * 批量物理删除
     *
     * @param Ids
     */
    public ServerResponse deleteBatchTrue(List<Integer> Ids) {
        return userMapper.deleteBatchUserTrue(Ids) == Ids.size() ? new ServerResponse(ResponseCode.SUCCESS, "批量物理删除成功") : new ServerResponse(ResponseCode.ERROR, "批量物理删除失败");
    }

    /**
     * 批量逻辑删除
     *
     * @param Ids
     * @return
     */
    public ServerResponse deleteBatchLogical(List<Integer> Ids) {
        return userMapper.deleteBatchUserLogical(Ids) == Ids.size() ? new ServerResponse(ResponseCode.SUCCESS, "批量逻辑删除成功") : new ServerResponse(ResponseCode.ERROR, "批量逻辑删除失败");
    }

    /**
     * 逻辑恢复
     *
     * @param user
     * @return
     */
    public ServerResponse recoverLogical(User user) {
        user.setIsDeleted(0);
        return userMapper.updateByPrimaryKeySelective(user) == 1 ? new ServerResponse(ResponseCode.SUCCESS, "逻辑恢复成功") : new ServerResponse(ResponseCode.ERROR, "逻辑恢复失败");

    }

    //查询所有非病人列表
    public ServerResponse getPersonList(PageParam<User> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<User> userList = userMapper.superSearchNoPatient(pageParam.getSuperSearchKeyWord());
            PageInfo<User> drugPageInfo = new PageInfo<User>(userList);
            return new ServerResponse(ResponseCode.SUCCESS, drugPageInfo);
        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<User> userList = userMapper.notPatientList();
            PageInfo<User> drugPageInfo = new PageInfo<User>(userList);
            return new ServerResponse(ResponseCode.SUCCESS, drugPageInfo);
        }
    }

    /**
     * 排班表列表
     * @param pageParam
     * @return
     */
    public ServerResponse getSchedulingList(PageParam<Scheduling> pageParam){
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<Scheduling> schedulingList = schedulingMapper.superSearchList(pageParam.getSuperSearchKeyWord());
            PageInfo<Scheduling> schedulingPageInfo = new PageInfo<Scheduling>(schedulingList);
            return new ServerResponse(ResponseCode.SUCCESS, schedulingPageInfo);
        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<Scheduling> schedulingList = schedulingMapper.schedulingList();
            PageInfo<Scheduling> schedulingPageInfo = new PageInfo<Scheduling>(schedulingList);
            return new ServerResponse(ResponseCode.SUCCESS, schedulingPageInfo);
        }
    }

    /**
     * 权限列表
     *
     * @return
     */
    public ServerResponse getRoleList() {
        List<Role> roles = roleMapper.roleList();
        return new ServerResponse(ResponseCode.SUCCESS, roles);
    }

    /**
     * 添加排班
     * @param scheduling
     * @return
     */

    public ServerResponse addScheduling(Scheduling scheduling){
        int i = schedulingMapper.insertSelective(scheduling);
        if(i>0){
            return  new ServerResponse(ResponseCode.SUCCESS,"排班成功");
        }else {
            return  new ServerResponse(ResponseCode.ERROR,"排班失败");
        }
    }

    /**
     * 修改排班
     * @param scheduling
     * @return
     */
    public ServerResponse updateScheduling(Scheduling scheduling){
        int i = schedulingMapper.updateByPrimaryKeySelective(scheduling);
        if(i>0){
            return  new ServerResponse(ResponseCode.SUCCESS,"调班成功");
        }else {
            return  new ServerResponse(ResponseCode.ERROR,"调班失败");
        }
    }

    /**
     * 删除排班
     * @param id
     * @return
     */
    public ServerResponse deleteScheduling(Integer id){

        int i = schedulingMapper.deleteByPrimaryKey(id);
        if(i>0){
            return  new ServerResponse(ResponseCode.SUCCESS,"删除成功");
        }else {
            return  new ServerResponse(ResponseCode.ERROR,"删除失败");
        }
    }
}
